package com.china08.yxyapi.api.home.index.slider;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeSlider;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeSliderRepository;

/**
 * 首页--焦点图查询
 * 
 */
@CrossOrigin
@RestController
public class IndexSrhSliderController extends YxyApiBaseController {

    @Autowired
    private YxyHomeSliderRepository yxyHomeSliderRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/slider", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) RepModel reqModel) {

        String schoolId = reqModel.schoolId;
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        if (schoolId != null) {
            List<YxyHomeSlider> yxyHomeSliderLst = yxyHomeSliderRepository.findTop5ByEdbIdAndStatusOrderByOrderNumDesc(schoolId, 0);

            if (yxyHomeSliderLst.size() == 0) {
                RespModel respModel1 = new RespModel();
                RespModel respModel2 = new RespModel();
                RespModel respModel3 = new RespModel();
                respModel1.url = "#";
                respModel2.url = "#";
                respModel3.url = "#";
                respModel1.img = "/static/img/silder/default-1.jpg";
                respModel2.img = "/static/img/silder/default-2.jpg";
                respModel3.img = "/static/img/silder/default-3.jpg";
                respModelLst.add(respModel1);
                respModelLst.add(respModel2);
                respModelLst.add(respModel3);
                return respModelLst;
            }

            for (YxyHomeSlider yxyHomeSlider : yxyHomeSliderLst) {
                RespModel respModel = new RespModel();
                respModel.img = yxyHomeSlider.getImg();
                respModel.url = yxyHomeSlider.getUrl();
                respModelLst.add(respModel);
            }
        }
        return respModelLst;
    }

    protected static class RepModel extends YxyApiReqModel {
        public String domain;
        public String schoolId;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String img;
        public String url;
    }
}
